﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace State
{
    //流程控制
    public class FlowContext
    {
        public bool OverFlag { get; set; }

        //0： 通过，1：驳回 2：退回整改  3：已申请
        public int Status { get; set; }

        //节点信息
        public NodeBase NodeInfo { get; set; }

        public string RequstMessage { get; set; }


        //开始发起审批流程
        public bool Start(FlowContext context)
        {
            NodeBase node = new LeadNode();
            context.NodeInfo = node;
            context.Status = 3;
            context.NodeInfo.NodeHandle(context);
            bool approvalFlag = false;
            if ("Hr李".Equals(node.NodeName)&&context.Status==0&&context.OverFlag)
            {
                approvalFlag = true;
                Console.WriteLine("审批通过！");
            }
            else
            {
                Console.WriteLine("审批未通过，流程结束。");
            }
            return approvalFlag;
        }

    }

    //节点父类
   public abstract class NodeBase
    {
        public string NodeName { get; set; }
        public abstract void NodeHandle(FlowContext context);
    }

    public class LeadNode : NodeBase
    {
        public override void NodeHandle(FlowContext context)
        {
            if ( context == null||context.OverFlag)
            {
                Console.WriteLine("审批流程结束。");
                return;
            }
            Console.WriteLine("申请内容是：{0}",context.RequstMessage);
            if (context.Status != 3)
            {
                Console.WriteLine("审批流程结束。");
            }
            else
            {
                this.NodeName = "张经理";
                context.RequstMessage = context.RequstMessage + NodeName + "审核通过";
                context.Status = 0;
                context.NodeInfo = new HrNode("Hr李");
                context.NodeInfo.NodeHandle(context);
            }
        }
    }

    public class HrNode : NodeBase
    {
        public HrNode(string nameStr)
        {
            NodeName = nameStr;
        }
        public override void NodeHandle(FlowContext context)
        {
            if (context == null || context.OverFlag)
            {
                Console.WriteLine("审批流程结束。");
                return;
            }
            if (context.Status==0)
            {
                Console.WriteLine(context.RequstMessage+NodeName+"审核通过");
                context.Status = 0;
                context.OverFlag = true;
            }
            else
            {
                Console.WriteLine("审批流程结束。");
            }
        }
    }

}
